<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Weixin\Controller;

use User\Api\UserApi;

/**
 * 用户控制器
 * 包括用户中心，用户登录及注册
 */
class KoubeiController extends WeixinController
{
    public function band_wx()
    {

        if (IS_POST) {
            $one = D('Ptyh')->where(['uid' => $this->uid])->find();
            if ($one['wx_uid'] != 0) {
                echo "该用户已绑定过微信号";
                die;

            }
            $id = D('Ptyh')->where(['uid' => $this->uid])->save(['wx_uid' => $this->wx_uid]);
            if ($id > 0) {
                echo 1;
                die;
            } else {
                echo 0;
                die;
            }

        } else {
            C('WEB_SITE_TITLE', '微信绑定');
            $this->display('Koubei/band');
        }
    }

    public function unband_wx()
    {
        if (IS_POST) {
            $one = D('Ptyh')->where(['uid' => $this->uid])->find();
            if ($one['wx_uid'] != 0 && $one['wx_uid'] != $this->wx_uid) {
                echo "该用户已通过另一微信号绑定，请使用绑定的微信号解除";
                die;

            }
            $id = D('Ptyh')->where(['uid' => $this->uid])->save(['wx_uid' => 0]);
            if ($id > 0) {
                echo 1;
                die;
            } else {
                echo 0;
                die;

            }

        } else {
            C('WEB_SITE_TITLE', '微信解绑');
            $this->display('Koubei/band');
        }
    }

    function cookie($var, $value = '', $time = 0, $path = '', $domain = '')
    {
        $_COOKIE[$var] = $value;
        if (is_array($value)) {
            foreach ($value as $k => $v) {
                setcookie($var . '[' . $k . ']', $v, $time, $path, $domain, $s);
            }
        } else {
            setcookie($var, $value, $time, $path, $domain, $s);
        }
    }

    public function login()
    {

        if (IS_POST) {
            $account = I('post.account');
            $password = I('post.password');
            $one = D('Ptyh')->where(['account' => $account])->find();//同一个账号只存在一个，否则无法区分使用哪个账号登录

            if (empty($one)) {
                echo 0;
                die;
            } else {
                $p = md5($password);
                if ($p != $one['password']) {
                    echo "密码错误";
                    die;
                } else {

                    $data = array(
                        'account' => md5($account),
                        'login' => 1,
                        'uid' => $one['uid'],
                    );

                    $this->cookie('user', $data, time() + 3600 * 24);

                    $this->uid = $one['uid'];


                    echo 1;
                    die;

                }
            }

        } else {

            C('WEB_SITE_TITLE', '登录');
            $this->display();
        }
    }

    public function logout()
    {
        // unset($_SESSION['user']);
        // $data=$_COOKIE['user'];
        $this->cookie('user', '', time() - 1);
        C('WEB_SITE_TITLE', '登录');
        $this->display('login');

    }

    public function forget()
    {

        // if($this->wx_uid!=31){
        //     print_r('维护中，请稍后重试');die;
        // }

        if (IS_POST) {
            $account = I('post.account');
            $np = I('post.npassword');
            $code = I('post.code');

            $yh = D('Ptyh')->where(['account' => $account])->find();
            if (empty($yh)) {
                print_r('该账号不存在');
                die;
            }
            if (empty($np)) {
                print_r('请输入密码');
                die;
            }
            $active = D('Active')->where(['phone' => $account, 'type' => 2])->find();

            if (!empty($active)) {
                if ($code == $active['code']) {
                    D('Active')->where(['phone' => $account, 'type' => 2])->delete();
                    $id = D('Ptyh')->where(['account' => $account])->save(['password' => md5($np)]);
                    print_r($id);
                    die;
                } else {
                    print_r('验证码错误');
                    die;
                }
            } else {
                print_r('请填入验证码');
                die;
            }

        } else {
            C('WEB_SITE_TITLE', '忘记密码');
            $this->display();
        }
    }

    // function export(){
    //     $uid=$this->uid;

    //     $type=I('get.type');
    //     if($type=='tixian_btn'){
    //         $uid=$this->uid;
    //         $list=D('Ptyh')->where("uid=".$uid)->find();
    //         $gather=D('Gather')->where("uid=".$uid)->find();
    //         C('WEB_SITE_TITLE','提现');
    //         $this->assign('list',$list);
    //         $this->assign('gather',$gather);
    //     }

    // }

    /**
     * 个人中心首页
     */
    public function index()
    {
//        $WeixinLogin = cookie('WeixinLogin');
//        dump($WeixinLogin['openid']);exit;
        // echo '暂不开放';die;
        $uid = $this->uid;
        // print_r($uid);die;
        $one = D('Ptyh')->where('uid=' . $uid)->find();
        if (empty($one['name'])) {
            echo "<script>confirm('请先绑定您的信息！');location.href='http://qnm.upslope.cn/index.php?m=Weixin&c=Koubei&a=yonghu'</script>";
            die;

        }
        // 数据整合开始
        if ($uid > 0) {
            D('Linshi')->where(['uid' => $uid])->delete();
        }

        $a = D('Dingdan')->where(['uid' => $one['uid']])->select();//订单
        $b = D('Dividend')->where(['uid' => $one['uid']])->select(); //分润
        $c = D('Extract')->where(['uid' => $one['uid']])->select(); //提现
        $d = D('Split')->where(['uid' => $one['uid']])->select(); //口碑

        $e = D('Youka')->where(['uid' => $one['uid']])->select(); //油卡
        $f = D('Xyk')->where(['uid' => $one['uid']])->select(); //信用卡

        $lists = array();
        $key = 0;
        foreach ($a as $v) {//收益
            $lists[$key]['uid'] = $v['uid'];
            $lists[$key]['danhao'] = $v['danhao'];
            $lists[$key]['trade_no'] = $v['trade_no'];
            $lists[$key]['total'] = $v['pay_money'];
            $lists[$key]['money'] = $v['yh_money'];//当时的收益，费率可能会变
            $lists[$key]['t_money'] = $v['t_money'];


            $lists[$key]['type'] = 1;
            $lists[$key]['time'] = $v['pay_time'];
            $lists[$key]['status'] = $v['status'];
            $key++;

        }
        foreach ($b as $v) {//分润
            $lists[$key]['uid'] = $v['uid'];
            $lists[$key]['danhao'] = $v['danhao'];
            $lists[$key]['trade_no'] = $v['trade_no'];
            $lists[$key]['total'] = $v['total'];
            $lists[$key]['money'] = $v['divide'];
            $lists[$key]['type'] = 2;
            $lists[$key]['time'] = $v['time'];
            $lists[$key]['status'] = $v['status'];
            $lists[$key]['t_money'] = $v['t_money'];
            $key++;

        }

        foreach ($c as $v) {//提现
            $lists[$key]['uid'] = $v['uid'];
            $lists[$key]['danhao'] = $v['out_biz_no'];
            $lists[$key]['trade_no'] = $v['order_id'];
            $lists[$key]['total'] = $v['money'];
            $lists[$key]['money'] = $v['money'];//到账金额
            $lists[$key]['type'] = 3;
            $lists[$key]['time'] = $v['time'];
            $lists[$key]['status'] = $v['status'];
            $lists[$key]['t_money'] = $v['t_money'];
            $key++;

        }

        foreach ($d as $v) {//口碑收益
            // $one=D('Dingdan')->where(['trade_no'=>$v['trade_no']])->find();
            $lists[$key]['uid'] = $v['uid'];
            $lists[$key]['danhao'] = $v['out_request_no'];
            $lists[$key]['trade_no'] = $v['trade_no'];
            $lists[$key]['total'] = $v['money_in'];
            $lists[$key]['money'] = $v['sh_money'];
            $lists[$key]['t_money'] = $v['t_money'];
            $lists[$key]['type'] = 4;
            $lists[$key]['time'] = $v['time'];
            $lists[$key]['status'] = $v['status'];
            $key++;

        }
        // print_r($e);
        foreach ($e as $v) {//油卡
            // print_r($v);
            $lists[$key]['uid'] = $v['uid'];
            $lists[$key]['danhao'] = $v['orderId'];
            $lists[$key]['total'] = $v['face'];
            $lists[$key]['money'] = $v['yh_money'];//当时的收益，费率可能会变
            $lists[$key]['t_money'] = $v['t_money'];


            $lists[$key]['type'] = 5;
            $lists[$key]['time'] = $v['time'];
            $lists[$key]['status'] = $v['re_status'];
            $key++;

        }

        foreach ($f as $v) {//收益
            $lists[$key]['uid'] = $v['uid'];
            $lists[$key]['danhao'] = $v['danhao'];
            $lists[$key]['trade_no'] = $v['trade_no'];
            $lists[$key]['total'] = $v['pay_money'];
            $lists[$key]['money'] = $v['yh_money'];//当时的收益，费率可能会变
            $lists[$key]['t_money'] = $v['t_money'];


            $lists[$key]['type'] = 1;
            $lists[$key]['time'] = $v['pay_time'];
            $lists[$key]['status'] = $v['status'];
            $key++;

        }

        // print_r($lists);die;
        foreach ($lists as $v) {
            $one = D('Linshi')->where(['danhao' => $v['danhao']])->find();
            if (empty($one)) {
                D('Linshi')->add($v);
            }
        }

        // 数据整合结束
        C('WEB_SITE_TITLE', ' 创世|商户端控制台');
        $gather = D('Gather')->where('uid=' . $this->uid)->find();
        $one = D('Member')->where('uid=' . $this->wx_uid)->find();
        $ptyh = D('Ptyh')->where('uid=' . $this->uid)->find();
        $kbsh = D('Kbsh')->where('uid=' . $this->uid)->find(); //口碑商户

        $gg = D('Gg')->order('time desc')->limit(1)->find();


        // 账单
        $start = strtotime(I('get.start_time'));
        $died = strtotime(I('get.died_time'));
        $status = I('get.status');
        $type = I('get.type');
        $danhao = I('get.danhao');
        $export = I('get.export', false);
        $where = array();


        if (!empty(I('get.start_time')) && !empty(I('get.died_time'))) {
            $p = 0;
            $time = array('between', array($start, $died));
        } elseif (!empty(I('get.start_time')) && empty(I('get.died_time'))) {
            $p = 0;
            $time = array('gt', $start);
        } elseif (!empty(I('get.died_time')) && empty(I('get.start_time'))) {
            $p = 0;
            $time = array('lt', $died);
        }
        if (!empty($time)) {
            $p = 0;
            $where['time'] = $time;
        }
        if (!empty($danhao)) {
            $p = 0;
            if (strlen($danhao) == 28) {
                $where['trade_no'] = $danhao;
            } else {
                $where['danhao'] = $danhao;
            }

        }
        if (!empty($status) && $status > -1) {
            $p = 0;
            $where['status'] = $status;
        }
        if (!empty($type) && $type > -1) {
            $p = 0;
            $where['type'] = $type;
        }

        $where['uid'] = $uid;
        $dingdan_list = D('Linshi')->where($where)->order('time desc')->select();
        $this->assign('dingdan_list', $dingdan_list);
        // 账单-------------------

        // 团队-------------------
        $where2 = array();
        $str = I('get.tuanyuan');
        if (!empty($str)) {
            $p = 3;
            $where2['yh_uid'] = $str;
        }

        $where2['status'] = 1;
        $where2['pid'] = $uid;
        $team_list = D('Ptyh')->where($where2)->order('uid desc')->select();
        $_this = D('Ptyh')->where("uid=$uid")->find();
        $this_pid = $_this['pid'];
        if ($this_pid == 0 || $this_pid == 1) {
            $pid = 0;   //没有上级或者就是老用户
        } else {
            // 新用户  有上级
            $pid = D('Ptyh')->where("uid=$this_pid")->find();
        }
        $gg_this = D('Gather')->where("uid=$uid")->find();
        $this->assign('pid', $pid);
        $this->assign('_this', $_this);
        $this->assign('gg_this', $gg_this);
        $this->assign('team_list', $team_list);


        // if(empty($p)){
        //     $p=2;
        // }


        $this->assign('p', $p);
        $this->assign('gg', $gg);
        $this->assign('uid', $uid);
        $this->assign('one', $one);
        $this->assign('gather', $gather);
        $this->assign('ptyh', $ptyh);
        $this->display();
    }

    /**
     * 个人中心首页
     */
    public function index2()
    {


        $uid = $this->uid;
        // print_r($uid);die;
        $one = D('Ptyh')->where('uid=' . $uid)->find();
        // print_r($uid);
        // if(empty($one['name'])){
        //     echo "<script>confirm('请先绑定您的信息！');location.href='http://qnm.upslope.cn/index.php?m=Weixin&c=Koubei&a=yonghu'</script>";die;

        // }

        // 数据整合开始
        if ($uid > 0) {
            D('Linshi')->where(['uid' => $uid])->delete();
        }

        $a = D('Dingdan')->where(['uid' => $one['uid']])->select();
        $b = D('Dividend')->where(['uid' => $one['uid']])->select();
        $c = D('Extract')->where(['uid' => $one['uid']])->select();
        $d = D('Split')->where(['uid' => $one['uid']])->select();

        $e = D('Youka')->where(['uid' => $one['uid']])->select();


        $lists = array();
        $key = 0;
        foreach ($a as $v) {//收益
            $lists[$key]['uid'] = $v['uid'];
            $lists[$key]['danhao'] = $v['danhao'];
            $lists[$key]['trade_no'] = $v['trade_no'];
            $lists[$key]['total'] = $v['pay_money'];
            $lists[$key]['money'] = $v['yh_money'];//当时的收益，费率可能会变
            $lists[$key]['t_money'] = $v['t_money'];


            $lists[$key]['type'] = 1;
            $lists[$key]['time'] = $v['pay_time'];
            $lists[$key]['status'] = $v['status'];
            $key++;

        }
        foreach ($b as $v) {//分润
            $lists[$key]['uid'] = $v['uid'];
            $lists[$key]['danhao'] = $v['danhao'];
            $lists[$key]['trade_no'] = $v['trade_no'];
            $lists[$key]['total'] = $v['total'];
            $lists[$key]['money'] = $v['divide'];
            $lists[$key]['type'] = 2;
            $lists[$key]['time'] = $v['time'];
            $lists[$key]['status'] = $v['status'];
            $lists[$key]['t_money'] = $v['t_money'];
            $key++;

        }


        foreach ($c as $v) {//提现
            $lists[$key]['uid'] = $v['uid'];
            $lists[$key]['danhao'] = $v['out_biz_no'];
            $lists[$key]['trade_no'] = $v['order_id'];
            $lists[$key]['total'] = $v['money'];
            $lists[$key]['money'] = $v['money'];//到账金额
            $lists[$key]['type'] = 3;
            $lists[$key]['time'] = $v['time'];
            $lists[$key]['status'] = $v['status'];
            $lists[$key]['t_money'] = $v['t_money'];
            $key++;

        }

        foreach ($d as $v) {//口碑收益
            // $one=D('Dingdan')->where(['trade_no'=>$v['trade_no']])->find();
            $lists[$key]['uid'] = $v['uid'];
            $lists[$key]['danhao'] = $v['out_request_no'];
            $lists[$key]['trade_no'] = $v['trade_no'];
            $lists[$key]['total'] = $v['money_in'];
            $lists[$key]['money'] = $v['sh_money'];
            $lists[$key]['t_money'] = $v['t_money'];
            $lists[$key]['type'] = 4;
            $lists[$key]['time'] = $v['time'];
            $lists[$key]['status'] = $v['status'];
            $key++;

        }
// print_r($e);
        foreach ($e as $v) {//油卡
            // print_r($v);
            $lists[$key]['uid'] = $v['uid'];
            $lists[$key]['danhao'] = $v['orderId'];
            $lists[$key]['total'] = $v['face'];
            $lists[$key]['money'] = $v['yh_money'];//当时的收益，费率可能会变
            $lists[$key]['t_money'] = $v['t_money'];


            $lists[$key]['type'] = 5;
            $lists[$key]['time'] = $v['time'];
            $lists[$key]['status'] = $v['re_status'];
            $key++;

        }


// print_r($lists);die;
        foreach ($lists as $v) {
            $one = D('Linshi')->where(['danhao' => $v['danhao']])->find();
            if (empty($one)) {
                D('Linshi')->add($v);
            }

        }
        // 数据整合结束


        C('WEB_SITE_TITLE', ' 创世|商户端控制台');
        $gather = D('Gather')->where('uid=' . $this->uid)->find();
        $one = D('Member')->where('uid=' . $this->wx_uid)->find();
        $ptyh = D('Ptyh')->where('uid=' . $this->uid)->find();
        $kbsh = D('Kbsh')->where('uid=' . $this->uid)->find();

        $gg = D('Gg')->order('time desc')->limit(1)->find();


        // 账单
        $start = strtotime(I('get.start_time'));
        $died = strtotime(I('get.died_time'));
        $status = I('get.status');
        $type = I('get.type');
        $danhao = I('get.danhao');
        $export = I('get.export', false);
        $where = array();


        if (!empty(I('get.start_time')) && !empty(I('get.died_time'))) {
            $p = 0;
            $time = array('between', array($start, $died));
        } elseif (!empty(I('get.start_time')) && empty(I('get.died_time'))) {
            $p = 0;
            $time = array('gt', $start);
        } elseif (!empty(I('get.died_time')) && empty(I('get.start_time'))) {
            $p = 0;
            $time = array('lt', $died);
        }
        if (!empty($time)) {
            $p = 0;
            $where['time'] = $time;
        }
        if (!empty($danhao)) {
            $p = 0;
            if (strlen($danhao) == 28) {
                $where['trade_no'] = $danhao;
            } else {
                $where['danhao'] = $danhao;
            }

        }
        if (!empty($status) && $status > -1) {
            $p = 0;
            $where['status'] = $status;
        }
        if (!empty($type) && $type > -1) {
            $p = 0;
            $where['type'] = $type;
        }

        $where['uid'] = $uid;
        $dingdan_list = D('Linshi')->where($where)->order('time desc')->select();


        // 账单-------------------

        // 团队-------------------
        $where2 = array();
        $str = I('get.tuanyuan');
        if (!empty($str)) {
            $p = 3;
            $where2['yh_uid'] = $str;
        }

        $where2['status'] = 1;
        $where2['pid'] = $uid;
        $team_list = D('Ptyh')->where($where2)->order('uid desc')->select();
        $_this = D('Ptyh')->where("uid=$uid")->find();
        $this_pid = $_this['pid'];
        if ($this_pid == 0 || $this_pid == 1) {
            $pid = 0;   //没有上级或者就是老用户
        } else {
            // 新用户  有上级
            $pid = D('Ptyh')->where("uid=$this_pid")->find();
        }
        $gg_this = D('Gather')->where("uid=$uid")->find();
        $this->assign('dingdan_list', $dingdan_list);
        $this->assign('pid', $pid);
        $this->assign('_this', $_this);
        $this->assign('gg_this', $gg_this);
        $this->assign('team_list', $team_list);
        $this->assign('p', $p);
        $this->assign('gg', $gg);
        $this->assign('uid', $uid);
        $this->assign('one', $one);
        $this->assign('gather', $gather);
        $this->assign('ptyh', $ptyh);
        $this->display();
    }

    /**
     * 码主绑定
     */
    public function shanghu_band()
    {

        $uid = $this->uid;

        $allow = D('Kballow')->where(['uid' => $uid])->find();

        if (empty($allow)) {
            echo "<script>alert('暂不开放');location.href=history.go(-1)</script>";
            die;
        }

        $one = D('Kbxx')->where('uid=' . $uid)->find();
        if (empty($one['name'])) {

        } else {
            echo "<script>location.href='http://qnm.upslope.cn/index.php?m=Weixin&c=Koubei&a=pdata1'</script>";
            die;
        }


        if (IS_POST) {
            // print_r($_POST);die;
            $model = D('Kbxx');
            $_POST['uid'] = $this->uid;

            if (empty($_POST['name'])) {
                $this->error('请填写姓名');
            }

            if (empty($_POST['phone'])) {
                $this->error('请填写手机号码！');
            }


            $data2 = [
                'uid' => $uid,
                'money' => 0,
                'count' => 0,
                'status' => 1,
            ];

            $gather = M('Gather');
            $_one = $gather->where('uid=' . $uid)->find();
            if (empty($_one)) {
                $id2 = $gather->add($data2);
            }


            $one = $model->where('uid=' . $this->uid)->find();

            if (!empty($one)) {
                $this->redirect('pdata1');
            }
            $id = $model->add($_POST);

            if ($id > 1) {
            } else {
                $this->error('上传失败，请重新上传。');
            }


        } else {

            C('WEB_SITE_TITLE', '商户绑定');
            $this->display();
        }

    }

    // 商户入驻的授权二维码
    public function shanghu()
    {
        $uid = $this->uid;  //链接上增加商户的uid  以便知道是哪个商户授权

        $one = D('Kbxx')->where('uid=' . $uid)->find();
        if (empty($one)) {
            $this->redirect('shanghu_band');//没有这个商户  需要先上传身份证去认证
        } else {

            if (empty($one['name'])) {
                //有这个商户  但是没有上传身份证信息
                $this->redirect('shanghu_band');
            } else if (!empty($one['name']) && $one['real_status'] == 0) {
                // 有这个商户 已上传身份证 但未通过验证
                $this->error('您的认证信息已上传，请耐心等候人工审核~');
            } else {
                // 有这个商户 已通过身份验证
                //
            }
        }


        $url = "https://openauth.alipay.com/oauth2/appToAppAuth.htm?app_id=2018030502317646&redirect_uri=http://qnm.upslope.cn/pay.php&uid={$uid}";
        $url = urlencode($url);

        $this->assign('url', $url);
        C('WEB_SITE_TITLE', '供码入驻');
        $this->display();
    }

    // 平台用户的收款码
    public function erwm()
    {
        $one = D('Ptyh')->where('uid=' . $this->uid)->find();
        $gather = D('Gather')->where('uid=' . $this->uid)->find();
        if ($one['identify'] == 0) {
            echo "<script>confirm('请先绑定您的信息！');location.href='http://qnm.upslope.cn/index.php?m=Weixin&c=Koubei&a=yonghu'</script>";
            die;

        }

        if (empty($one['status'])) {
            echo "<script>confirm('您的认证信息有待审核，请耐心等候！');location.href='http://qnm.upslope.cn/index.php?m=Weixin&c=Koubei&a=index'</script>";
            die;

        }

        $type = I('get.type');
        if (empty($type)) {
            $type = 1;

        }
        $uid = $this->uid;
        if ($type == 1) {

            $url = "http://qnm.upslope.cn/index.php?m=weixin&c=user&a=yh_payment2&uid={$uid}";
        } else if ($type == 2) {
            if ($gather['status_fq'] == 1 || $gather['status_hb'] == 1 || $gather['status_xy'] == 1) {

                $url = "http://qnm.upslope.cn/index.php?m=weixin&c=user&a=yh_payment3&uid={$uid}";
            } else {
                echo "<script>confirm('您没有权限使用普通码！');location.href='http://qnm.upslope.cn/index.php?m=weixin&c=koubei&a=erwm'</script>";
                die;
            }
        }

        $url = urlencode($url);
        $this->assign('url', $url);
        $this->assign('type', $type);

        $this->assign('one', $one);
        C('WEB_SITE_TITLE', '创世-商户收款码');
        $this->display();
    }

    // 平台用户的收款码
    public function erwm2()
    {
        $one = D('Ptyh')->where('uid=' . $this->uid)->find();
        $gather = D('Gather')->where('uid=' . $this->uid)->find();
        if ($one['identify'] == 0) {
            echo "<script>confirm('请先绑定您的信息！');location.href='http://qnm.upslope.cn/index.php?m=Weixin&c=Koubei&a=yonghu'</script>";
            die;

        }

        if (empty($one['status'])) {
            echo "<script>confirm('您的认证信息有待审核，请耐心等候！');location.href='http://qnm.upslope.cn/index.php?m=Weixin&c=Koubei&a=index'</script>";
            die;

        }


        $type = I('get.type');
        if (empty($type)) {
            $type = 1;

        }
        $uid = $this->uid;
        if ($type == 1) {

            $url = "http://qnm.upslope.cn/index.php?m=weixin&c=user&a=yh_payment2&uid={$uid}";
        } else if ($type == 2) {
            if ($gather['status_fq'] == 1 || $gather['status_hb'] == 1 || $gather['status_xy'] == 1) {

                $url = "http://qnm.upslope.cn/index.php?m=weixin&c=user&a=yh_payment3&uid={$uid}";
            } else {
                echo "<script>confirm('您没有权限使用普通码！');location.href='http://qnm.upslope.cn/index.php?m=weixin&c=koubei&a=erwm'</script>";
                die;
            }
        }

        $url = urlencode($url);
        $this->assign('url', $url);
        $this->assign('type', $type);

        $this->assign('one', $one);
        C('WEB_SITE_TITLE', '创世-商户收款码');
        $this->display();
    }


    // 在线收款 开关
    public function shou_inline()
    {
        $gather = D('Gather')->where('uid=' . $this->uid)->find();
        if ($gather['status'] == 0) {
            D('Gather')->where('uid=' . $this->uid)->save(['status' => 1]);
        } else if ($gather['status'] == 1) {
            D('Gather')->where('uid=' . $this->uid)->save(['status' => 0]);
        }
        echo 1;
        die;
        // $one= D('Member')->where('uid='.$this->uid)->find();
        // $gather=D('Gather')->where('uid='.$this->uid)->find();
        // C('WEB_SITE_TITLE','口碑中心');
        // $this->assign('one',$one);
        // $this->assign('gather',$gather);
        // $this->display('Koubei/index');
    }

    function details_data()
    {


        $type = I('get.type');
        $uid = $this->uid;
        $one = D('Ptyh')->where("uid=" . $this->uid)->find();
        $two = D('Kbxx')->where("uid=" . $this->uid)->find();

        $this->assign('one', $one);
        $this->assign('type', $type);
        $this->assign('two', $two);

        if ($type == 1) {
            C('WEB_SITE_TITLE', '实名认证');
        } else if ($type == 2) {
            C('WEB_SITE_TITLE', '个人信息');
        } else if ($type == 3) {
            C('WEB_SITE_TITLE', '支付宝修改');
        } else if ($type == 4) {
            C('WEB_SITE_TITLE', '密码修改');
        }


        $this->display();
    }


    // 用户信息
    public function pdata1()
    {
        if (IS_POST) {
            $phone = I('post.phone');
            $id = D('Ptyh')->where("phone=" . $phone)->save(I('post.'));

            echo $id;
            die;
        } else {
            $one = D('Ptyh')->where("uid=" . $this->uid)->find();
            $two = D('Kbxx')->where("uid=" . $this->uid)->find();
            $this->assign('one', $one);
            $this->assign('two', $two);

            C('WEB_SITE_TITLE', '用户信息');
            $this->display();
        }


    }

    // 用户信息
    public function pdata2()
    {
        if (IS_POST) {
            $phone = I('post.phone');
            $id = D('Ptyh')->where("phone=" . $phone)->save(I('post.'));

            echo $id;
            die;
        } else {
            $one = D('Ptyh')->where("uid=" . $this->uid)->find();
            $two = D('Kbxx')->where("uid=" . $this->uid)->find();
            $this->assign('one', $one);
            $this->assign('two', $two);

            C('WEB_SITE_TITLE', '用户信息');
            $this->display();
        }


    }

    public function tixian()
    {
        $model = D('Extract');
        if (IS_POST) {
            $start = strtotime(I('post.start_time'));
            $died = strtotime(I('post.died_time'));
            $status = I('post.status');
            $where = array();

            if (!empty(I('post.start_time')) && !empty(I('post.died_time'))) {

                $pay_time = array('between', array($start, $died));
            } elseif (!empty(I('post.start_time')) && empty(I('post.died_time'))) {

                $pay_time = array('gt', $start);
            } elseif (!empty(I('post.died_time')) && empty(I('post.start_time'))) {

                $pay_time = array('lt', $died);
            }
            if (!empty($pay_time)) {
                $where['time'] = $pay_time;
            }
            if (!empty($danhao)) {

                $where['out_biz_no'] = $danhao;
            }
            if ($status != -1) {

                $where['status'] = $status;
            }

            $where['uid'] = $this->uid;

            $list = $this->lists($model, $where);


        } else {
            $list = $this->lists($model, ['uid' => $this->uid]);
            // $list=$model->where("uid=".$this->uid)->select();

        }

        $uid = $this->uid;
        $total = count($model->where("uid=" . $this->uid)->select());
        $success = count($model->where(['uid' => $uid, 'status' => 1])->select());
        $defeat = count($model->where(['uid' => $uid, 'status' => 0])->select());
        $money = $model->where(['uid' => $this->uid, 'status' => 1])->sum('money');
        $this->assign('total', $total);
        $this->assign('success', $success);
        $this->assign('defeat', $defeat);
        $this->assign('money', $money);

        C('WEB_SITE_TITLE', '提现记录');
        $this->assign('list', $list);
        $this->display();

    }


    /*
 *     微信获取图片
 *
 */
    function get_weixin_pic($media_id, $model = false, $where = false, $field = false)
    {
        $pathinfo = pathinfo($media_id);
        if (!empty($pathinfo['extension'])) {
            return $media_id;
        }
        $options = array(
            'token' => 'Xxshtong',   // 填写你设定的key
            'appid' => 'wxccfa8bbdd1cdd046',    // 填写高级调用功能的appid
            'appsecret' => 'a6b51ac38ab5b78e9cb4fd33ded04fe4' // 填写高级调用功能的密钥
        );
        $weixin = new \Common\Extend\Wechat\Wechat($options);
        $url = $weixin->downloadPic($media_id);
        if (!empty($model)) {
            if (!empty($url)) {
                M($model)->where($where)->save([$field => $url]);
            }
        }
        return $url;
    }


    public function verify()
    {


        if (IS_POST) {
            // print_r($_POST);die;
            $model = D('Ptyh');
            $phone = I('post.phone');
            $shop_name = I('post.shop_name');
            $alipay = I('post.alipay');
            $tg_num = I('post.tg_num');
            $name = I('post.name');
            $identify = I('post.identify');


            $p = I('post.password');
            $np = I('post.npassword');

            $arr1 = ['XYK', 'xyk', 'TX', 'tx', 'HB', 'hb', '金融', '花', '呗', '套', '现', '信用卡', '无视', '风', '控', '专家'];
            foreach ($arr1 as $key => $v) {
                if (strpos($shop_name, $v) !== false) {
                    echo "店铺名字包含敏感字，请重新输入！";
                    die;
                }
            }

            $py = D('Ptyh')->where(['tg_num' => $tg_num])->find();
            $ptdata = D("Ptdata")->where(['id' => 1])->find();
            if ($tg_num == $ptdata['pt_tgnum']) {

            } else {
                if (empty($py) || strlen($tg_num) < 9) {
                    // 根据推广码查不到上级
                    echo "您输入的推广码有误！";
                    die;
                }
            }


            if ($p != $np) {
                echo "两次密码输入不一致";
                die;
            }
            $xt = D('Ptyh')->where(['identify' => $identify])->select();
            if (count($xt) > 2) {
                // 相同身份证号不超过3个
                echo '该身份证使用次数已满！';
                die;
            }


            if (empty($phone)) {
                echo "手机号码！";
                die;

            }

            $yh = D('Ptyh')->where(['account' => $phone])->find();
            if (!empty($yh)) {
                // 根据推广码查不到上级
                echo "手机号已被注册";
                die;
            }

            if (empty($shop_name)) {

                echo "店铺名称！";
                die;

            }
            if (empty($alipay)) {

                echo "支付宝账号！";
                die;
            }
            echo 1;
            die;

            // $data['id_name'] = I('post.name');
            // $data['id_no']   = I('post.identify');

            // $_one=D('Real')->where(['identify'=>I('post.identify')])->find();
            // if(!empty($_one)){
            //     echo 1;die;
            // }else{
            //     $res = curl_send_post($data);
            //     if($res['ret_code']=='000000'){
            //         $data2=array(
            //             'name'=>I('post.name'),
            //             'identify'=>I('post.identify'),
            //         );
            //         D('Real')->add($data2);
            //         echo 1;die;
            //     }else if($res['ret_code']=='000001'){
            //         echo "姓名与身份证号不匹配";die;
            //     }else{
            //         echo "系统异常，请联系客服处理";die;
            //     }
            // }


        }

    }

    public function verify2()
    {
        print_r($_POST);
        die;
        $data['id_name'] = I('post.name');
        $data['id_no'] = I('post.identify');

        $_one = D('Real')->where(['identify' => I('post.identify')])->find();
        if (!empty($_one)) {
            echo 1;
            die;
        } else {
            $res = curl_send_post($data);
            if ($res['ret_code'] == '000000') {
                $data2 = array(
                    'name' => I('post.name'),
                    'identify' => I('post.identify'),
                );
                D('Real')->add($data2);
                echo 1;
                die;
            } else if ($res['ret_code'] == '000001') {
                echo "姓名与身份证号不匹配";
                die;
            } else {
                echo "系统异常，请联系客服处理";
                die;
            }
        }


    }

// 平台用户绑定信息
    public function yonghu2()
    {

        // if($this->wx_uid!=33&&$this->wx_uid!=31){
        //     $this->error('资料认证暂不开放');
        // }
        if (IS_POST) {

            // print_r($_POST);die;
            $code = I('post.code');
            $phone = I('post.phone');
            $active = D('Active')->where(['phone' => $phone, 'type' => 1])->find();
            if (empty($code)) {
                echo "请输入手机验证码";
                die;
            } else {
                $active = D('Active')->where(['phone' => $phone, 'type' => 1])->find();
                if (($active['time'] + 1800) > time()) {

                    if ($code == $active['code']) {
                        // 验证码正确
                        // echo "验证码正确";die;
                        D('Active')->where(['phone' => $phone, 'type' => 1])->delete();
                    } else {
                        echo "验证码错误";
                        die;

                    }
                } else {

                    echo "验证码已过期";
                    die;
                }


            }

            $tg_num = I('post.tg_num');
            $py = D('Ptyh')->where(['tg_num' => $tg_num])->find();
            $data = I('post.');
            unset($data['code']);
            unset($data['npassword']);
            if ($py['level'] == 3) {

                $data['password'] = md5(I('post.password'));
                $data['pid'] = $py['uid'];
                $data['level'] = $py['level'] + 1;
                $data['yh_uid'] = date("Ymd") . rand(100000, 999999);
                $data['account'] = $phone;
                $data['status'] = 1;
            } else {
                $data['password'] = md5(I('post.password'));
                $data['pid'] = $py['uid'];
                $data['level'] = $py['level'] + 1;
                $data['yh_uid'] = date("Ymd") . rand(100000, 999999);
                $data['account'] = $phone;
                $data['status'] = 1;
                $data['tg_num'] = rand(100000000, 999999999);

            }


            $gather = D('Gather')->where(['uid' => $py['uid']])->find(); //上级
            $up_set = $gather['up_set'] + 0.5;
            $up_set2 = $gather['up_set2'] + 0.2;
            $data2 = [
                'money' => 0,
                'count' => 0,
                'status' => 1,
                'up_set' => $up_set,
                'up_set2' => $up_set2,
                'level' => $py['level'] + 1,
            ];
            $gather = M('Gather');

            $id2 = $gather->add($data2);
            if ($id2 > 0) {//gather存储成功再去存储用户信息

                $uid = D('Ptyh')->add($data);
                if ($uid > 0) {
                    $id = $gather->where(['id' => $id2])->save(['uid' => $uid]);
                    echo $id;
                    die;
                } else {
                    // 保存不成功不需要删除用户信息，提示再次上传即可
                    D('Gather')->where('id=' . $id2)->delete();
                    echo '上传失败，请稍后重试！';
                    die;
                }
            } else {
                D('Gather')->where('id=' . $id2)->delete();
                echo '上传失败，请稍后重试！';
                die;
            }

        } else {


            $model = D('Ptyh');
            $this->uid = $_SESSION['user']['uid'];
            if (empty($_SESSION['user']['uid'])) {
                // 未登录 未获取到用户uid
            } else {
                $uid = $this->uid;
                $one = $model->where("uid=" . $uid)->find();
                if (!empty($one['identify'])) {
                    $this->redirect('pdata1');
                }
            }

            $tgm = $_GET['tgm'];
            $this->assign('tgm', $tgm);
            C('WEB_SITE_TITLE', '商户入驻');
            $this->display();

        }


    }


// 平台用户绑定信息(注册)
    public function yonghu()
    {

        // if($this->wx_uid!=33&&$this->wx_uid!=31){
        //     $this->error('资料认证暂不开放');
        // }
        if (IS_POST) {

            // print_r($_POST);die;
            $code = I('post.code');
            $phone = I('post.phone');
            $active = D('Active')->where(['phone' => $phone, 'type' => 1])->find();
            if (empty($code)) {
                echo "请输入手机验证码";
                die;
            } else {
                $active = D('Active')->where(['phone' => $phone, 'type' => 1])->find();
                if (($active['time'] + 1800) > time()) {

                    if ($code == $active['code']) {
                        // 验证码正确
                        // echo "验证码正确";die;
                        D('Active')->where(['phone' => $phone, 'type' => 1])->delete();
                    } else {
                        echo "验证码错误";
                        die;

                    }
                } else {
                    D('Active')->where(['phone' => $phone, 'type' => 1])->delete();
                    echo "验证码已过期";
                    die;
                }
            }

            $tg_num = I('post.tg_num');

            $data = I('post.');
            unset($data['code']);
            unset($data['npassword']);

            $ptdata = D("Ptdata")->where(['id' => 1])->find();
            if ($tg_num == $ptdata['pt_tgnum']) {
                // 一级
                $data['password'] = md5(I('post.password'));
                $data['pid'] = 0;
                $data['level'] = 1;
                $data['yh_uid'] = $this->getUserCode();
                $data['account'] = $phone;
                $data['status'] = 1;
                $data['tg_num'] = rand(100000000, 999999999);
            } else {
                //上级数据
                $py = D('Ptyh')->where(['tg_num' => $tg_num])->find();
                if ($py['level'] >= 3) {

                    $data['password'] = md5(I('post.password'));
                    $data['pid'] = $py['uid'];
                    $data['level'] = $py['level'] + 1;
                    $data['yh_uid'] = $this->getUserCode();
                    $data['account'] = $phone;
                    $data['status'] = 1;
                } else {
                    $data['password'] = md5(I('post.password'));
                    $data['pid'] = $py['uid'];
                    $data['level'] = $py['level'] + 1;
                    $data['yh_uid'] = $this->getUserCode();
                    $data['account'] = $phone;
                    $data['status'] = 1;
                    $data['tg_num'] = rand(100000000, 999999999);
                }
            }


            if ($tg_num == $ptdata['pt_tgnum']) {

                $data['path1'] = $ptdata['pt_rate'];
                $up_set = $ptdata['pt_rate'];
                $up_set2 = $ptdata['pt_rate2'];
                $data2 = [
                    'money' => 0,
                    'count' => 0,
                    'status' => 1,
                    'up_set' => $up_set,
                    'up_set2' => $up_set2,
                    'path1' => $ptdata['pt_rate'],
                    'path2' => $ptdata['pt_rate2'],
                    'level' => $py['level'] + 1,
                ];

            } else {

                $gather = D('Gather')->where(['uid' => $py['uid']])->find(); //上级

//                $up_set = $gather['up_set'] + 0.5;
//                $up_set2 = $gather['up_set2'] + 0.2;
                $up_set = $py['child_hb_rate'];
                $up_set2 = $py['child_xy_rate'];

                $data2 = [
                    'money' => 0,
                    'count' => 0,
                    'status' => 1,
                    'up_set' => $up_set,
                    'up_set2' => $up_set2,
                    'path1' => $ptdata['pt_rate'],
                    'path2' => $ptdata['pt_rate2'],
                    'level' => $py['level'] + 1,
                ];

            }


            $gather = M('Gather');

            $id2 = $gather->add($data2);
            if ($id2 > 0) {//gather存储成功再去存储用户信息

                $data['wx_uid'] = $this->wx_uid;
                $uid = D('Ptyh')->add($data);
                if ($uid > 0) {
                    $id = $gather->where(['id' => $id2])->save(['uid' => $uid]);
                    echo $id;
                    die;
                } else {
                    // 保存不成功不需要删除用户信息，提示再次上传即可
                    D('Gather')->where('id=' . $id2)->delete();
                    echo '上传失败，请稍后重试！';
                    die;
                }
            } else {
                D('Gather')->where('id=' . $id2)->delete();
                echo '上传失败，请稍后重试！';
                die;
            }

        } else {
            $model = D('Ptyh');
            $this->uid = $_SESSION['user']['uid'];
            if (empty($_SESSION['user']['uid'])) {
                // 未登录 未获取到用户uid
            } else {
                $uid = $this->uid;
                $one = $model->where("uid=" . $uid)->find();
                if (!empty($one['identify'])) {
                    $this->redirect('pdata1');
                }
            }

            $tgm = $_GET['tgm'];
            $this->assign('tgm', $tgm);
            C('WEB_SITE_TITLE', '绑定平台');
            $this->display();
        }
    }


    public function change()
    {
        $p = I('post.password');
        $np = I('post.npassword');
        $code = I('post.code');
        $uid = $this->uid;
        $one = D('Ptyh')->where(['uid' => $uid])->find();
        // print_r($one);
        // echo $this->uid;die;
        if (empty($p) || empty($np)) {
            print_r('请填写密码');
            die;
        }
        if ($p == $np) {
            print_r('输入的新旧密码不可相同');
            die;
        }
        if (md5($p) != $one['password']) {
            print_r('旧密码输入错误');
            die;
        }
        if (md5($np) == $one['password']) {
            print_r('新旧密码不可相同');
            die;
        }
        $active = D('Active')->where(['phone' => $one['phone'], 'type' => 2])->find();
        // echo D('Active')->getlastsql();
        // print_r($active);die;
        if (empty($code)) {
            echo "请输入手机验证码";
            die;
        } else {
            if (($active['time'] + 180) > time()) {

                if ($code == $active['code']) {
                    // 验证码正确
                    D('Active')->where(['phone' => $one['phone'], 'type' => 2])->delete();
                    $id = D('Ptyh')->where(['uid' => $uid])->save(['password' => md5($np)]);
                    if ($id > 0) {
                        print_r(1);
                        die;
                    } else {
                        print_r(2);
                        die;
                    }
                } else {
                    echo "验证码错误";
                    die;
                }
            } else {
                echo "验证码已过期";
                die;
            }
        }
    }


    public function dingdan()
    {

        $uid = $this->uid;
        $one = D('Ptyh')->where('uid=' . $uid)->find();
        if (empty($one['name'])) {
            echo "<script>confirm('请先绑定您的信息！');location.href='http://qnm.upslope.cn/index.php?m=Weixin&c=Koubei&a=yonghu'</script>";
            die;

        }


        $model = D('Dingdan');
        $start = strtotime(I('get.start_time'));
        $died = strtotime(I('get.died_time'));
        $status = I('get.status');
        $type = I('get.type');
        $danhao = I('get.danhao');
        $export = I('get.export', false);
        $where = array();

        if (!empty(I('get.start_time')) && !empty(I('get.died_time'))) {

            $time = array('between', array($start, $died));
        } elseif (!empty(I('get.start_time')) && empty(I('get.died_time'))) {

            $time = array('gt', $start);
        } elseif (!empty(I('get.died_time')) && empty(I('get.start_time'))) {

            $time = array('lt', $died);
        }
        if (!empty($time)) {
            $where['time'] = $time;
        }
        if (!empty($danhao)) {
            if (strlen($danhao) == 28) {
                $where['trade_no'] = $danhao;
            } else {
                $where['danhao'] = $danhao;
            }

        }
        if ($status != -1) {

            $where['status'] = $status;
        }
        if ($type != -1) {

            $where['type'] = $type;
        }

        $where['uid'] = $this->uid;
// 数据整合开始
        $a = D('Dingdan')->where(['uid' => $uid])->select();
        $b = D('Dividend')->where(['uid' => $uid])->select();
        $c = D('Extract')->where(['uid' => $uid])->select();
        $d = D('Split')->where(['uid' => $uid])->select();
        $lists = array();
        $key = 0;
        foreach ($a as $v) {//收益
            $lists[$key]['uid'] = $v['uid'];
            $lists[$key]['danhao'] = $v['danhao'];
            $lists[$key]['trade_no'] = $v['trade_no'];
            $lists[$key]['total'] = $v['pay_money'];
            $lists[$key]['money'] = $v['yh_money'];//当时的收益，费率可能会变
            $lists[$key]['t_money'] = $v['t_money'];


            $lists[$key]['type'] = 1;
            $lists[$key]['time'] = $v['pay_time'];
            $lists[$key]['status'] = $v['status'];
            $key++;

        }
        foreach ($b as $v) {//分润
            $lists[$key]['uid'] = $v['uid'];
            $lists[$key]['danhao'] = $v['danhao'];
            $lists[$key]['trade_no'] = $v['trade_no'];
            $lists[$key]['total'] = $v['total'];
            $lists[$key]['money'] = $v['divide'];
            $lists[$key]['type'] = 2;
            $lists[$key]['time'] = $v['time'];
            $lists[$key]['status'] = $v['status'];
            $lists[$key]['t_money'] = $v['t_money'];
            $key++;

        }


        foreach ($c as $v) {//提现
            $lists[$key]['uid'] = $v['uid'];
            $lists[$key]['danhao'] = $v['out_biz_no'];
            $lists[$key]['trade_no'] = $v['order_id'];
            $lists[$key]['total'] = $v['money'];
            $lists[$key]['money'] = $v['money'];//到账金额
            $lists[$key]['type'] = 3;
            $lists[$key]['time'] = $v['time'];
            $lists[$key]['status'] = $v['status'];
            $lists[$key]['t_money'] = $v['t_money'];
            $key++;

        }

        foreach ($d as $v) {//口碑收益
            // $one=D('Dingdan')->where(['trade_no'=>$v['trade_no']])->find();
            $lists[$key]['uid'] = $v['uid'];
            $lists[$key]['danhao'] = $v['out_request_no'];
            $lists[$key]['trade_no'] = $v['trade_no'];
            $lists[$key]['total'] = $v['money_in'];
            $lists[$key]['money'] = $v['sh_money'];
            $lists[$key]['t_money'] = $v['t_money'];
            $lists[$key]['type'] = 4;
            $lists[$key]['time'] = $v['time'];
            $lists[$key]['status'] = $v['status'];
            $key++;

        }

        foreach ($lists as $v) {
            $one = D('Linshi')->where(['danhao' => $v['danhao']])->find();
            if (empty($one)) {
                D('Linshi')->add($v);
            }

        }
// 数据整合结束


        $list = $this->lists('Linshi', $where, 'time desc');


        $uid = $this->uid;
        $total1 = D('Dingdan')->where(['uid' => $uid, 'status' => 1])->sum('yh_money');     //订单获得
        $total2 = D('Dividend')->where(['uid' => $uid, 'status' => 1])->sum('divide'); //推广获得
        $total3 = D('Split')->where(['uid' => $uid, 'status' => 1])->sum('sh_money'); //口碑收益获得
        $total = $total1 + $total2 + $total3;

        $money = D('Extract')->where(['uid' => $uid, 'status' => 1])->sum('money');
        $this->assign('total', $total);
        $this->assign('money', $money);


        C('WEB_SITE_TITLE', '账单中心');
        $this->assign('list', $list);
        $this->display();

    }

    function dingdan_view()
    {
        $danhao = I('get.id');
        $type = I('get.type');
        if ($type == 1) {
            // 订单
            $info = D('Dingdan')->where(['danhao' => $danhao])->find();
            if (empty($info)) {
                $info = D('Xyk')->where(['danhao' => $danhao])->find();
            }
        } else if ($type == 2) {
            // 分润
            $info = D('Dividend')->where(['danhao' => $danhao])->find();
        } else if ($type == 3) {
            // 提现
            $info = D('Extract')->where(['out_biz_no' => $danhao])->find();
        } else if ($type == 4) {
            // 口碑分润
            $info = D('Split')->where(['out_request_no' => $danhao])->find();
        } else if ($type == 5) {
            // 油卡
            $info = D('Youka')->where(['orderId' => $danhao])->find();

        }

        // print_r($info);die;


        C('WEB_SITE_TITLE', '订单详情');
        $this->assign('info', $info);
        $this->assign('type', $type);
        $this->display();
    }


    public function tixian_btn()
    {
        if (IS_POST) {
        } else {
            $list = $this->lists('Ptyh', ["uid" => $this->uid]);
            $gather = D('Gather')->where("uid=" . $this->uid)->find();
            C('WEB_SITE_TITLE', '提现');
            $this->assign('list', $list);
            $this->assign('gather', $gather);
            $this->display();
        }
    }


    // 我的费率
    public function rate()
    {
        if (IS_POST) {
            $path6 = I('post.path6');
            $path5 = I('post.path5');
            $one = D('Gather')->where(['uid' => $this->uid])->find();

            if ($path6 < 5) {
                echo "花呗支付费率不可低于5%！";
                die;
            } else if ($path6 > 15) {
                echo "花呗支付费率不可高于15%！";
                die;
            }
            if ($path5 < 5) {
                echo "花呗分期支付费率不可低于5%！";
                die;
            } else if ($path5 > 10) {
                echo "花呗分期支付费率不可高于10%！";
                die;
            }


            $id = D('Gather')->where(['uid' => $this->uid])->save(['path6' => $path6, 'path5' => $path5]);
            if ($id > 0) {
                echo "1";
                die;
            } else {
                echo "0";
                die;
            }

        } else {
            $gather = D('Gather')->where('uid=' . $this->uid)->find();
            $kbsh = D('Kbsh')->where('uid=' . $this->uid)->select();
            $yh = D('Ptyh')->where('pid=' . $this->uid)->select();


            // print_r($kbsh);die;

            C('WEB_SITE_TITLE', '我的费率');
            $this->assign('kbsh', $kbsh);
            $this->assign('yh', $yh);
            $this->assign('gather', $gather);
            $this->display();
        }
    }

    // 我的费率
    public function rate2()
    {
        if (IS_POST) {
            $path6 = I('post.path6');
            $path7 = I('post.path7');
            $one = D('Gather')->where(['uid' => $this->uid])->find();
            if ($path6 < 5) {
                echo "花呗支付费率不可低于5%！";
                die;
            } else if ($path6 > 15) {
                echo "花呗支付费率不可高于15%！";
                die;
            }
            if ($path7 < 5) {
                echo "花呗分期支付费率不可低于5%！";
                die;
            } else if ($path7 > 15) {
                echo "花呗分期支付费率不可高于15%！";
                die;
            }


            $id = D('Gather')->where(['uid' => $this->uid])->save(['path6' => $path6, 'path7' => $path7]);
            if ($id > 0) {
                echo "1";
                die;
            } else {
                echo "0";
                die;
            }

        } else {
            $gather = D('Gather')->where('uid=' . $this->uid)->find();
            $kbsh = D('Kbsh')->where('uid=' . $this->uid)->select();
            $yh = D('Ptyh')->where('pid=' . $this->uid)->select();


            // print_r($kbsh);die;

            C('WEB_SITE_TITLE', '我的费率');
            $this->assign('kbsh', $kbsh);
            $this->assign('yh', $yh);
            $this->assign('gather', $gather);
            $this->display();
        }
    }


    /**
     * 常见问题
     */
    public function question()
    {

        C('WEB_SITE_TITLE', '常见问题');
        $this->display();
    }

    public function gg_list()
    {

        $list = D('Gg')->order('time desc')->select();

        $this->assign('list', $list);

        C('WEB_SITE_TITLE', '历史公告');

        $this->display();
    }

    public function pcreate()
    {
        $uid = $this->uid;
        $one = D('Ptyh')->where('uid=' . $uid)->find();
        $gather = D('Gather')->where('uid=' . $uid)->find();
        if (empty($one)) {
            echo "<script>confirm('请先绑定您的信息！');location.href='http://qnm.upslope.cn/index.php?m=Weixin&c=Koubei&a=yonghu'</script>";
            die;
            // $this->error('请先去-平台绑定-绑定您的信息。');
        }

        // 判断某用户是否达到条件，达到条件则可以使用该收款方式
        // 交易量达10万
        $_this = D('Dingdan')->where(['status' => 1, 'uid' => $uid])->sum('pay_money');

        if ($_this < 100000) {
            if ($uid == 222) {

            } else {
                echo "<script>confirm('您好，您的商户暂未达到我司使用花呗分期收款准入标准！');history.go(-1)</script>";
                die;
            }

        }


        $url = "http://qnm.upslope.cn/index.php?m=weixin&c=user&a=yh_pcreate&uid={$uid}";

        $url = urlencode($url);
        $this->assign('url', $url);

        $this->assign('one', $one);
        C('WEB_SITE_TITLE', '花呗分期');
        $this->display();
    }


    public function tgm()
    {
        $uid = $this->uid;
        $one = D('Ptyh')->where("uid=$uid")->find();
        if (empty($one['identify'])) {
            echo "<script>confirm('请先去-商户入驻-绑定您的信息。');history.go(-1)</script>";
            die;
        }
        $url = "http://qnm.upslope.cn/index.php?m=weixin&c=koubei&a=tgm_band&uid={$uid}";

        $url = urlencode($url);
        $this->assign('url', $url);
        $this->assign('one', $one);
        C('WEB_SITE_TITLE', '推广码');
        $this->display();
    }

    public function tgm2()
    {
        // print_r($this);die;
        $uid = $this->uid;
        if ($this->uid != 2) {
            echo "<script>confirm('推广码暂停使用');history.go(-1)</script>";
            die;
        }
        $one = D('Ptyh')->where("uid=$uid")->find();
        $url = "http://qnm.upslope.cn/index.php?m=weixin&c=koubei&a=tgm_band&uid={$uid}";

        $url = urlencode($url);
        $this->assign('url', $url);
        $this->assign('one', $one);
        C('WEB_SITE_TITLE', '推广码');
        $this->display();
    }


    /**
     * 商户授权码绑定
     */
    public function tgm_band()
    {
        $pid = $_GET['uid'];  //上级的uid
        echo $pid;
        $yh = D('Ptyh')->where(['uid' => $pid])->find();
        $tgm = $yh['tg_num']; //推广数字码
        echo "<script>location.href='http://qnm.upslope.cn/index.php?m=weixin&c=koubei&a=yonghu&tgm=$tgm';</script>";
        die;
    }

    public function team()
    {
        if (IS_POST) {
            $uid = I('post.uid');
            $path2_status = I('post.status');
            if ($uid > 1) {
                //  !!!!!!!!!!!!!!关闭自己的下级连同所有下级的下级全都关闭!!!!!!!!!!!!!
                $id = D('Gather')->where("uid={$uid}")->save(['path2_status' => $path2_status]);

                if ($id > 0) {
                    // 上级关闭成功  接下来将下级全部关闭
                    if ($path2_status == 0) {
                        $list = D('Ptyh')->select();
                        $result = $this->subtree($list, $uid);
                        foreach ($result as $key => $v) {
                            D('Gather')->where("uid=$v")->save(['path2_status' => $path2_status]);
                        }
                    }

                    echo 1;
                    die;
                } else {
                    echo 0;
                    die;
                }
            }
        } else {
            $uid = $this->uid;
            $list = $this->lists('Ptyh', ['pid' => $uid, 'status' => 1], 'uid desc');
            $one = D('Ptyh')->where("uid=$uid")->find();
            $this_pid = $one['pid'];
            if ($this_pid == 0 || $this_pid == 1) {
                $pid = 0;   //没有上级或者就是老用户
            } else {
                // 新用户  有上级
                $pid = D('Ptyh')->where("uid=$this_pid")->find();
            }
            $one = D('Gather')->where("uid=$uid")->find();
            $this->assign('pid', $pid);
            $this->assign('one', $one);
            $this->assign('list', $list);
            C('WEB_SITE_TITLE', '我的团队');
            $this->display();
        }

    }

    public function team2()
    {
        if (IS_POST) {

            $uid = I('post.uid');
            $frozen = I('post.status');
            $up_set = I('post.up_set');
            $up_set2 = I('post.up_set2');

            if ($uid > 1) {
                // 单独修改

                $_this = D('Gather')->where(['uid' => $this->uid])->find();
                $up_set = (float)$up_set;
                $up_set2 = (float)$up_set2;
                if (!empty($up_set) && $up_set >= $_this['up_set']) {
                    if ($up_set <= ($_this['up_set'] + 1)) {

                        $data['up_set'] = $up_set;
                    } else {
                        echo '设置的费率范围不可高于您费率的1%';
                        die;
                    }

                } else if (empty($up_set)) {

                } else {
                    echo '设置的费率范围不可小于您自身的费率';
                    die;
                }


                if (!empty($up_set2) && $up_set2 >= $_this['up_set2']) {
                    if ($up_set2 <= ($_this['up_set2'] + 0.2)) {
                        $data['up_set2'] = $up_set2;
                    } else {
                        echo '设置的费率范围不可高于您费率的0.2%';
                        die;
                    }

                } else if (empty($up_set2)) {

                } else {
                    echo '设置的费率范围不可小于您自身的费率';
                    die;
                }

                $id = D('Gather')->where(['uid' => $uid])->save($data);
                echo $id;
                die;


            } else if ($uid == 1 && !empty($up_set)) {
                // 全部修改

                $_this = D('Gather')->where(['uid' => $this->uid])->find();
                $up_set = (float)$up_set;
                if ($up_set >= $_this['up_set']) {
                    if ($up_set <= ($_this['up_set'] + 1)) {

                    } else {
                        echo '设置的费率范围不可高于您费率的1%';
                        die;
                    }

                } else {
                    echo '设置的费率范围不可小于您自身的费率';
                    die;
                }

                if ($uid == 1) {
                    // 全部修改
                    $list = D('Ptyh')->where(['pid' => $this->uid])->select();
                    foreach ($list as $key => $v) {
                        D('Gather')->where(['uid' => $v['uid']])->save(['up_set' => $up_set]);
                    }
                    echo 1;
                    die;

                } else {
                    // 修改单一
                    $id = D('Gather')->where(['uid' => $uid])->save(['up_set' => $up_set]);
                    echo $id;
                    die;
                }

            }
        }
    }

// 递归查找下级下级下级------------------------------------------
    function subtree($arr, $uid)
    { //id表示我们最初查找的id
        static $subs = array();
        foreach ($arr as $v) {

            if ($v['pid'] == $uid) {
                $subs[] = $v['uid'];
                $this->subtree($arr, $v['uid']); //最重要的一步
            }
        }
        return $subs;
    }


    public function team_2()
    {
        if (IS_POST) {
            $uid = I('post.uid');
            $path2_status = I('post.status');
            if ($uid > 1) {
                //  !!!!!!!!!!!!!!关闭自己的下级连同所有下级的下级全都关闭!!!!!!!!!!!!!
                $id = D('Gather')->where("uid={$uid}")->save(['path2_status' => $path2_status]);

                if ($id > 0) {
                    // 上级关闭成功  接下来将下级全部关闭
                    if ($path2_status == 0) {
                        $list = D('Ptyh')->select();
                        $result = $this->subtree($list, $uid);
                        foreach ($result as $key => $v) {
                            D('Gather')->where("uid=$v")->save(['path2_status' => $path2_status]);
                        }
                    }

                    echo 1;
                    die;
                } else {
                    echo 0;
                    die;
                }
            }
        } else {
            $uid = $this->uid;
            $list = $this->lists('Ptyh', ['pid' => $uid, 'identify' => array('gt', 0)], 'id desc');
            $one = D('Ptyh')->where("uid=$uid")->find();
            $this_pid = $one['pid'];
            if ($this_pid == 0 || $this_pid == 1) {
                $pid = 0;   //没有上级或者就是老用户
            } else {
                // 新用户  有上级
                $pid = D('Ptyh')->where("uid=$this_pid")->find();
            }
            $one = D('Gather')->where("uid=$uid")->find();
            $this->assign('pid', $pid);
            $this->assign('one', $one);
            $this->assign('list', $list);
            C('WEB_SITE_TITLE', '我的团队');
            $this->display();
        }

    }


    public function divide()
    {
        if (IS_POST) {

        } else {
            $uid = $this->uid;
            $one = D('Ptyh')->where("uid=$uid")->find();

            $parent = $one['name'] . '-' . $one['shop_name'];
            $list = $this->lists('Dividend', ['parent' => $parent], 'id desc');

            $this->assign('list', $list);
            C('WEB_SITE_TITLE', '我的团队');
            $this->display();
        }

    }

    /**
     * 条码收款(当面付)
     */
    function bar()
    {
        // 限制条件

        $one = D('Gather')->where(['uid' => $this->uid])->find();
        if ($one['status_tm'] == 0) {
            echo '<script>alert("该商户无此收款权限！");location.href="http://qnm.upslope.cn/index.php?m=&&m=Weixin&c=Koubei&a=index"</script>';
            die;
        }


        if (IS_POST) {
            // print_r(I('post.'));die;
            // $authCode='289164163735558058';
            // $totalAmount='0.1';


            $authCode = I('post.auth_code');
            $totalAmount = I('post.money');
            $one = D('Gather')->where(['uid' => $this->uid])->find();

            $ptyh = D('Ptyh')->where(['uid' => $this->uid])->find();
            $money = $totalAmount * (1 - $one['path4'] / 100);//不回款


            $outTradeNo = 'tm' . date("YmdHis") . rand(1000, 9999);
            $subject = '条码支付' . $ptyh['shop_name'];


            $data = [
                'uid' => $this->uid,
                'danhao' => $outTradeNo,
                'pay_money' => $totalAmount,
                'pay_time' => time(),
            ];
            D('Dingdan')->add($data);
            Vendor('dangmianfu_demo_php.f2fpay.barpay_test');
            $barpay = new \Barpay;
            $result = $barpay->barpay($outTradeNo, $subject, $totalAmount, $authCode);
            $result = $result->alipay_trade_pay_response;
            $code = $result->code;


            if ($code == '10003') {
                // //10003 此错误含义是支付处理中，使用查询接口查询订单状态。
                $result = $this->order_query($barpay, $outTradeNo);
                if ($result->code == '10000' && $result->trade_status == 'TRADE_SUCCESS') {
                    // 支付成功
                    $data = [
                        'buyer' => $result->buyer_logon_id,
                        'trade_no' => $result->trade_no,
                        'status' => 1,
                        'yh_money' => $money,
                    ];

                    $id = D('Dingdan')->where(['danhao' => $outTradeNo])->save($data);
                    D('Gather')->where(['uid' => $this->uid])->setInc('money', $money); //增加余额

                    // 查找上一级
                    $ptyh = D('Ptyh')->where(['uid' => $this->uid])->find();
                    $pid = $ptyh['pid'];
                    $shou = $ptyh['uid'];
                    $danhao = 'tm' . date("YmdHis") . rand(1000, 9999);

                    $divide = [
                        'uid' => $pid,
                        'danhao' => $danhao,
                        'trade_no' => $result->trade_no,
                        'shou' => $shou,
                        'parent' => $pid,
                        'total' => $total,
                    ];
                    // 分润
                    D('Dividend')->add($divide);

                    //支付成功提醒
                    $data = [
                        'money' => $totalAmount,
                        'date' => date('Y-m-d H:i'),
                        'remark' => "收款订单号：" . $outTradeNo . '，付款金额：' . $totalAmount . '，付款方：' . $result->buyer_logon_id,
                    ];
                    $member = D('Member')->where(['yh_uid' => $one['uid']])->find();
                    if (!empty($member)) {
                        D('Notice')->weixin(10, $member['uid'], $data);
                        // print_r($member);die;
                    } else {
                        // echo '未绑定微信';die;
                    }
                    // D('Notice')->weixin(10,$one['uid'],$data);

                    print_r('收款成功！');
                    die;

                } else {
                    print_r($result->trade_status);
                    die;
                }

            } else if ($code == '10000' && $result->msg == 'Success') {
                // 支付成功
                $data = [
                    'buyer' => $result->buyer_logon_id,
                    'trade_no' => $result->trade_no,
                    'status' => 1,
                    'yh_money' => $money,
                ];
                $id = D('Dingdan')->where(['danhao' => $outTradeNo])->save($data);
                // 修改用户余额
                D('Gather')->where(['uid' => $this->uid])->setInc('money', $money);
                // 查找上一级
                $ptyh = D('Ptyh')->where(['uid' => $this->uid])->find();
                $pid = $ptyh['pid'];
                $shou = $ptyh['uid'];
                $danhao = date("YmdHis") . rand(100000, 999999);

                $divide = [
                    'uid' => $pid,
                    'danhao' => $danhao,
                    'trade_no' => $result->trade_no,
                    'shou' => $shou,
                    'parent' => $pid,
                    'total' => $total,
                ];
                // 分润
                D('Dividend')->add($divide);

                //支付成功提醒
                $data = [
                    'money' => $totalAmount,
                    'date' => date('Y-m-d H:i'),
                    'remark' => "收款订单号：" . $outTradeNo . '，付款金额：' . $totalAmount . '，付款方：' . $result->buyer_logon_id,
                ];
                $member = D('Member')->where(['yh_uid' => $one['uid']])->find();
                if (!empty($member)) {
                    D('Notice')->weixin(10, $member['uid'], $data);
                    // print_r($member);die;
                } else {
                    // echo '未绑定微信';die;
                }
                print_r('收款成功！');
                die;
            } else {
                print_r($result->sub_msg);
                die;
            }
        } else {


            $timestamp = time();
            $noncestr = $this->weixin->generateNonceStr();
            $jsapi_ticket = cookie('jsapi_ticket');
            if (empty($jsapi_ticket)) {
                $jsapi_ticket = $this->weixin->getticket();
                cookie('jsapi_ticket', $jsapi_ticket, 3600);
            }

            $config = [
                'noncestr' => $noncestr,
                'jsapi_ticket' => $jsapi_ticket,
                'timestamp' => $timestamp,
                'url' => get_url(),
            ];
            $i = 0;
            $stringToBeSigned = '';
            ksort($config);
            foreach ($config as $k => $v) {
                if ($i == 0) {
                    $stringToBeSigned .= "$k" . "=" . "$v";
                } else {
                    $stringToBeSigned .= "&" . "$k" . "=" . "$v";
                }
                $i++;
            }
            $signature = sha1($stringToBeSigned);
            $weixinconfig['nonceStr'] = $noncestr;
            $weixinconfig['signature'] = $signature;
            $weixinconfig['timestamp'] = $timestamp;
            $weixinconfig['appId'] = $this->appid;
            $this->assign('weixinconfig', $weixinconfig);
            $this->assign('uid', $this->uid);
            C('WEB_SITE_TITLE', '扫码支付');
            $this->display();
        }

    }


    function order_query($barpay, $outTradeNo)
    {
        $result = $barpay->barquery($outTradeNo);
        $result = $result->alipay_trade_query_response;
        return $result;
        if ($result->trade_status == 'WAIT_BUYER_PAY') {
            $this->order_query($barpay, $outTradeNo);
        } else {
            return $result;
        }
    }


    public function details()
    {

        $uid = $this->uid;
        $one = D('Ptyh')->where("uid=$uid")->find();

        $parent = $one['name'] . '-' . $one['shop_name'];
        $list = $this->lists('Dividend', ['parent' => $parent], 'id desc');

        $this->assign('list', $list);
        C('WEB_SITE_TITLE', '获润明细');
        $this->display();


    }


    public function trans_fault()
    {

        $uid = $this->uid;
        if ($uid == 2 || $uid == 3) {

            $model = D('Trans');
            $list = $model->where(['status' => 0, 'money' => array('gt', '2')])->select();
            C('WEB_SITE_TITLE', '失败记录');
            $this->assign('list', $list);
            $this->display();

        }


    }


    function dingdan_view2()
    {
        $danhao = I('get.id');
        $type = I('get.type');
        if ($type == 1) {
            // 订单
            $info = D('Dingdan')->where(['danhao' => $danhao])->find();
            if (empty($info)) {
                $info = D('Xyk')->where(['danhao' => $danhao])->find();
            }
        } else if ($type == 2) {
            // 分润
            $info = D('Dividend')->where(['danhao' => $danhao])->find();
        } else if ($type == 3) {
            // 提现
            $info = D('Extract')->where(['out_biz_no' => $danhao])->find();
        }

        // print_r($info);die;


        C('WEB_SITE_TITLE', '订单详情');
        $this->assign('info', $info);
        $this->assign('type', $type);
        $this->display();
    }

    // 单笔回款
    public function trans()
    {
        $id = I('post.id');
        $money = I('post.money');
        $out_biz_no = I('post.no');
        if (!empty($id) && !empty($money) && !empty($out_biz_no)) {

            $trans = D('Trans')->where(['out_biz_no' => $out_biz_no])->find();
            $ptyh = D('Ptyh')->where(['uid' => $trans['uid']])->find();
            Vendor('dangmianfu_demo_php.f2fpay.qrpay_test');
            $transfer = new \Alipay;
            $alipay = $id;
            $amount = $money - 1;
            $out_biz_no = $out_biz_no;
            $payer_show_name = $ptyh['name'];
            $remark = "转账";


            $time1 = strtotime(date("Ymd", strtotime("+1 day")));
            $time2 = strtotime(date("Ymd"));
            $td_money = D('Dingdan')->where("status=1 and pay_time between $time2 and $time1")->sum('pay_money');

            $adata = D('Adata')->where(['id' => 1])->find();
            // if(preg_match('/^[hft]/', $out_biz_no)){
            $result = $transfer->transfer5($out_biz_no, $alipay, $amount, $remark, $payer_show_name, $adata);

            // }else if(preg_match('/^[x]/', $out_biz_no)){
            //     $result=$transfer->transfer1($out_biz_no,$alipay,$amount,$remark,$payer_show_name);
            // }


            // if($td_money<950000){
            //     $result=$transfer->transfer5($out_biz_no,$alipay,$amount,$remark,$payer_show_name);
            // }else if($td_money>950000&&$td_money<1900000){
            //     $result=$transfer->transfer4($out_biz_no,$alipay,$amount,$remark,$payer_show_name);
            // }else if($td_money>1900000){
            //     $result=$transfer->transfer3($out_biz_no,$alipay,$amount,$remark,$payer_show_name);

            // }


            if (empty($result->sub_msg) && $result->out_biz_no == $out_biz_no) {
                // 说明是成功的   1、提现状态修改 2、提现次数、用户余额修改
                $trade_num = $result->order_id;
                D('Trans')->where(['out_biz_no' => $out_biz_no])->save(['status' => 1, 'trade_num' => $trade_num, 'time' => $result->pay_date]);
                echo "<script>confirm('转账成功！本次转出{$amount}元');location.href='http://qnm.upslope.cn/index.php?m=Weixin&c=Koubei&a=trans_fault'</script>";
                die;
            } else {
                if (preg_match('/^[x]/', $out_biz_no)) {
                    D('Tongdao')->where(['id' => 1])->save(['hk_xy' => 0]);
                }
                echo "<script>confirm('转账失败！原因：" . $result->sub_msg . "');location.href='http://qnm.upslope.cn/index.php?m=Weixin&c=Koubei&a=trans_fault'</script>";
                die;

            }
        }
    }


    public function close()
    {
        if (IS_POST) {
            $id = D('Tongdao')->where(['id' => 1])->save(I('post.'));
            if ($id > 0) {
                echo "<script>confirm('修改成功！');location.href='http://qnm.upslope.cn/index.php?m=Weixin&c=Koubei&a=close'</script>";
                die;
            } else {
                echo "<script>confirm('修改失败！');location.href='http://qnm.upslope.cn/index.php?m=Weixin&c=Koubei&a=close'</script>";
                die;
            }
        } else {
            $info = D('Tongdao')->find();
            $this->assign('info', $info);
            $this->meta_title = '通道管理';
            $this->display();
        }
    }


    /**
     * 商户上架下架设置
     */
    public function kbsh2()
    {
        if (IS_POST) {

            $id = I('post.id');
            $use_status = I('post.use_status');
            if ($use_status == 1) {
                $id = D('Kbsh')->where(['id' => $id])->save(['use_status' => 1, 'cause' => '']);

            } else if ($use_status == 0) {
                $id = D('Kbsh')->where(['id' => $id])->save(['use_status' => 0, 'cause' => '商户下架']);
            }
            // echo D('Kbsh')->getlastsql();die;
            ob_clean();
            print($id);
            die;


        }
    }

    /**
     * 修改商户供码费率
     */
    public function kbsh()
    {
        if (IS_POST) {
            $rate2 = I('post.rate2'); //商户供码费率
            $id = I('post.id');

            $kbsh = D('Kbsh')->where(['id' => $id])->find();
            if (!empty($rate2)) {

                if ($rate2 > $kbsh['rate']) {
                    echo "费率不可高于平台费率";
                    die;
                }
                if ($rate2 < 0.6) {
                    echo "费率不可低于0.6";
                    die;
                }
                $id = D('Kbsh')->where(['id' => $id])->save(['rate2' => $rate2]);
                print($id);
                die;
            }


        } else {
            $one = D('Kbxx')->where(['uid' => $this->uid])->find();
            $info = D('Kbsh')->where(['uid' => $this->uid])->order('create_time desc')->select();

            $count = count($info);
            $this->assign('count', $count);
            $this->assign('one', $one);
            $this->assign('info', $info);
            $this->meta_title = '码商系统';
            $this->display();
        }
    }

    /**
     * 修改商户供码费率
     */
    public function kbsh3()
    {
        if (IS_POST) {
            $rate2 = I('post.rate2');
            $id = I('post.id');

            $kbsh = D('Kbsh')->where(['id' => $id])->find();
            if (!empty($rate2)) {

                if ($rate2 > $kbsh['rate']) {
                    echo "费率不可高于平台费率";
                    die;
                }
                if ($rate2 < 0.6) {
                    echo "费率不可低于0.6";
                    die;
                }
                $id = D('Kbsh')->where(['id' => $id])->save(['rate2' => $rate2]);
                print($id);
                die;
            }


        } else {
            $one = D('Kbxx')->where(['uid' => $this->uid])->find();
            $info = D('Kbsh')->where(['uid' => $this->uid])->order('create_time desc')->select();

            $count = count($info);
            $this->assign('count', $count);
            $this->assign('one', $one);
            $this->assign('info', $info);
            $this->meta_title = '码商系统';
            $this->display();
        }
    }


// 发送短信验证码--修改，重置密码
    function get_code()
    {


        // print_r('验证码暂不使用，请晚点重试');die;


        $mobile = I('post.mobile');
        $type = I('post.type');
        if (empty($mobile)) {
            print_r('手机号码不可为空');
            die;
        }
        // type=1 资料认证  2 修改密码/重置密码  3 后台登录

        $yh = D('Ptyh')->where(['phone' => $mobile])->find();

        if (empty($yh)) {
            print_r('该用户不存在');
            die;
        }


        $one = D('Active')->where(['phone' => $mobile, 'type' => 2])->find();


        $code = '';


        if (!empty($one)) {
            if (($one['time'] + 180) > time()) {
                // 验证码仍有效

                print_r('验证码3分钟有效');
                die;
            } else {
                // 验证码失效
                $id = D('Active')->where(['phone' => $mobile, 'type' => 2])->delete();


                for ($i = 0; $i < 6; $i++) {
                    $code .= rand(1, 9);
                }
                $data = array(
                    'phone' => $mobile,
                    'code' => $code,
                    'time' => time(),
                    'type' => $type,
                );


                $id = D('Active')->add($data);
                if ($id > 0) {
                    $re = $this->send_post($code, $mobile, $type);
                    $re = json_decode($re, true);
                    if ($re['code'] == '000000') {
                        print_r(11);
                        die;
                    } else {
                        print_r($re['msg']);
                        die;
                    }
                }
            }

        } else {

            for ($i = 0; $i < 6; $i++) {
                $code .= rand(1, 9);
            }
            $data = array(
                'phone' => $mobile,
                'code' => $code,
                'time' => time(),
                'type' => $type,
            );


            $id = D('Active')->add($data);
            if ($id > 0) {
                $re = $this->send_post($code, $mobile, $type);
                $re = json_decode($re, true);
                if ($re['code'] == '000000') {
                    print_r(11);
                    die;
                } else {
                    print_r($re['msg']);
                    die;
                }
            }

        }
    }


    // 发送短信验证码--资料认证
    function zc_code()
    {
        // print_r($_POST);die;
//

        $mobile = I('post.mobile');
        $type = I('post.type');
        if (empty($mobile)) {
            print_r('手机号码不可为空');
            die;
        }
        // type=1 资料认证  2 修改密码/重置密码  3 后台登录
        // 一个手机号只能认证一次
        $yh = D('Ptyh')->where(['account' => $mobile])->find();
        if (!empty($yh)) {
            print_r('该手机号已被使用');
            die;
        }
        $one = D('Active')->where(['phone' => $mobile, 'type' => 1])->find();


        $code = '';


        if (!empty($one)) {
            if (($one['time'] + 180) > time()) {
                // 验证码仍有效

                print_r('验证码3分钟有效');
                die;
            } else {
                // 验证码失效
                D('Active')->where(['phone' => $mobile, 'type' => 1])->delete();


                for ($i = 0; $i < 6; $i++) {
                    $code .= rand(1, 9);
                }
                $data = array(
                    'phone' => $mobile,
                    'code' => $code,
                    'time' => time(),
                    'type' => $type,
                );


                $id = D('Active')->add($data);
                if ($id > 0) {
                    $re = $this->send_post($code, $mobile, $type);
                    $re = json_decode($re, true);
                    if ($re['code'] == '000000') {
                        print_r(11);
                        die;
                    } else {
                        print_r($re['msg']);
                        die;
                    }
                }
            }

        } else {

            for ($i = 0; $i < 6; $i++) {
                $code .= rand(1, 9);
            }
            $data = array(
                'phone' => $mobile,
                'code' => $code,
                'time' => time(),
                'type' => $type,
            );


            $id = D('Active')->add($data);
            if ($id > 0) {
                $re = $this->send_post($code, $mobile, $type);
                $re = json_decode($re, true);
                if ($re['code'] == '000000') {
                    print_r(11);
                    die;
                } else {
                    print_r($re['msg']);
                    die;
                }
            }

        }
    }

    // /**
    //  * 发送post请求
    //  * @param string $url 请求地址
    //  * @param array $post_data post键值对数据
    //  * @return string
    //  */
    function send_post($code, $mobile, $type)
    {

        require_once('ThinkPHP/Library/Vendor/sms/lib/Ucpaas.class.php');
        require_once('ThinkPHP/Library/Vendor/sms/serverSid.php');


        $appid = "0dce834d518d4c24b1a73e02453aee0c"; //应用的ID，可在开发者控制台内的短信产品下查看
        if ($type == 3) { // 1 认证资料  2 修改、重置 3 后台登录
            $templateid = "314554";    //可在后台短信产品→选择接入的应用→短信模板-模板ID，查看该模板ID
        } else if ($type == 2) {
            $templateid = "314525";
        } else if ($type == 1) {
            $templateid = "314526";
        }

        $param = $code; //多个参数使用英文逗号隔开（如：param=“a,b,c”），如为参数则留空
        $mobile = $mobile;
        $uid = "";

        //70字内（含70字）计一条，超过70字，按67字/条计费，超过长度短信平台将会自动分割为多条发送。分割后的多条短信将按照具体占用条数计费。

        return $ucpass->SendSms($appid, $templateid, $param, $mobile, $uid);
    }

    private function getUserCode()
    {
        return 'wx-' . date('md' . rand('0000', '9999'));
    }


}
